window.isPlayReady = 0;
$(function () {
	var MESSAGE_LIST = [
		'<img src="https://wujing123.oss-cn-beijing.aliyuncs.com/drink.jpeg?x-oss-process=style/mystyle" class="pic_shop">',
		'',
		'给你推荐一家店咖啡店，好喝到哭！',
		'这是不是给咱们迎新活动赞助咖啡券的那家店？<img src="https://wujing123.oss-cn-beijing.aliyuncs.com/wx032.png?x-oss-process=style/mystyle" class="faiEmjImg">',
		'对~对~我强烈推荐！南茶狗北九榀，不去尝尝后悔死你。',
		'走！走！现在就去！',
		'',
		'<img src="https://wujing123.oss-cn-beijing.aliyuncs.com/wx002.png?x-oss-process=style/mystyle" class="faiEmjImg"><img src="https://wujing123.oss-cn-beijing.aliyuncs.com/wx002.png?x-oss-process=style/mystyle" class="faiEmjImg">',
		'淡定淡定，改天再约，这两天加班<img src="https://wujing123.oss-cn-beijing.aliyuncs.com/wx027.png?x-oss-process=style/mystyle" class="faiEmjImg"><img src="https://wujing123.oss-cn-beijing.aliyuncs.com/wx027.png?x-oss-process=style/mystyle" class="faiEmjImg">',
		'好吧<img src="https://wujing123.oss-cn-beijing.aliyuncs.com/wx012.png?x-oss-process=style/mystyle" class="faiEmjImg">',
		'',
		'哎，对了，2018迎新活动就要开始 ，你报名了吗?',
		'肯定报了啊，这个活动我都期待好久了。我还想通过这次迎新活动聆听几位优秀学长带来的精彩演讲，学习他们成功的经验',
		'听他们的主题演讲确实让人受益匪浅，这次的主题演讲嘉宾都有哪些校友嘉宾呢？',
		'我想想，嗯~~',
		'',
		'一共有四位嘉宾，分别是张新育、陈维江、许琼林、王法承校友',
		'',
		'<img src="https://wujing123.oss-cn-beijing.aliyuncs.com/WechatIMG141.jpeg?x-oss-process=style/mystyle" class="pic_shop">',
		'都是重量级的嘉宾啊，其中的张新育校友就是为我们这次活动会务赞助的北京科锐的董事长，谢谢张总让我可以有机会领略中关村科技园区的魅力。',
		'确实是个难能可贵的机会，这次为我们赞助的还有通力有限公司。',
		'厉害了，真是要感谢两家优秀的校友企业。 哈哈，其实我这次去参加联谊会还有一个目的',
		'是抽到奖品满载而归吧',
		'你懂我<img src="https://wujing123.oss-cn-beijing.aliyuncs.com/wx006.png?x-oss-process=style/mystyle" class="faiEmjImg">',
		'可不是么，我懂你的肤浅。',
		'哈哈，我是挺白的，话说我是个中奖绝缘体，前一阵我转发了各种锦鲤，没一个中奖的。',
		'你把头像换成杨超越试试',
		'',
		'不过这次你肯定不会空手而归了',
		'难道？',
		'没错，就是你想的那样。Natural Park为我们所有到场的校友赞助了唇膏，每人一支，据说这唇膏是天然可食用的，纯天然无伤害，还特别好用。',
		'',
		'<img src="https://wujing123.oss-cn-beijing.aliyuncs.com/chungao1.jpeg?x-oss-process=style/mystyle" class="pic_shop">',
		'哇塞！这次真的赚！对于我这种中奖绝缘体来说真是莫大的福利。',
		'确实如此，其实今年我很期待自己可以抽中筑梦乡村赞助的白河湾青年号住宿券，看你们上次去玩的真不错。',
		'',
		'<img src="https://wujing123.oss-cn-beijing.aliyuncs.com/WechatIMG53.jpeg?x-oss-process=style/mystyle" class="pic_shop">',
		'青年号轰趴啊，那地方环境优美，住宿舒适，特别适合放松小聚。',
		'',
		'<img src="https://wujing123.oss-cn-beijing.aliyuncs.com/WechatIMG54.jpeg?x-oss-process=style/mystyle" class="pic_shop">',
		'',
		'<img src="https://wujing123.oss-cn-beijing.aliyuncs.com/WechatIMG55.jpeg?x-oss-process=style/mystyle" class="pic_shop">',
		'你越说我越想去，希望有好运气。',
		'话说还有其他大奖么，你快和我说说。',
		'<img class="pic_big " src="https://wujing123.oss-cn-beijing.aliyuncs.com/ADIIABAEGAAgooLQ3wUonrXIngIwhgI4igI.png?x-oss-process=style/mystyle">',
		1,
		2,
		'',
		'哈哈哈，谢老板！还有智泽联合控股赞助的华为mate20手机，博星证券赞助的iPad和运动手环。',
		'啥都不说了，我要转发一波HFUTBJ锦鲤，这次我一定能中奖',
		3
	];

	var IMAGE_URLS = [
		'https://wujing123.oss-cn-beijing.aliyuncs.com/0000042.jpg?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/wechat.png?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/wxhead.jpg?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/wxbottom.jpg?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/0000475.jpg?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/drink.jpeg?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/wx032.png?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/wx002.png?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/wx027.png?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/wx012.png?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/wx006.png?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/chungao1.jpeg?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/ADIIABAEGAAgooLQ3wUonrXIngIwhgI4igI.png?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/moneyFromMe.png?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/bonus.jpg?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/WechatIMG42.jpeg?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/WechatIMG43.jpeg?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/WechatIMG44.jpeg?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/WechatIMG53.jpeg?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/WechatIMG54.jpeg?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/WechatIMG55.jpeg?x-oss-process=style/mystyle',
		'https://wujing123.oss-cn-beijing.aliyuncs.com/WechatIMG141.jpeg?x-oss-process=style/mystyle'
	];
	var date = new Date();
	var hours = date.getHours();
	hours = hours < 9 ? '0'+hours : hours;
	var mins = date.getMinutes();
	mins = mins < 9 ? '0'+mins : mins;
	var mons = date.getMonth() + 1;
	mons = mons < 9 ? '0'+mons : mons;
	var days = date.getDate();
	days = days < 9 ? '0'+days : days;
	$('#page2 .time').html(hours + ':' + mins);
	$('#page2 .date').html(mons + '月' + days + '日 星期' + ['日','一','二','三','四','五','六'][date.getDay()]);
	$('#page3 .chat_time').html('星期' + ['日','一','二','三','四','五','六'][date.getDay()] + ' ' + mons + '月' + days + '日');
	$('#page4').css('transform', 'translateY('+document.documentElement.getBoundingClientRect().height+'px)');

	function App () {
		this.count = 1;
		this.I = null;
		this.imgcount = 0;
		this.scroll = null;
		this.scroll1 = null;
		this.isRun = false;
	}

	App.prototype = {
		constructor: App,
		init: function () {
			this.initScroll();
			this.initJSapi();
			this.bindEvent();
			this.loadImages();
			document.body.click();
		},
		initJSapi: function () {
			$.ajax({
				url: 'https://worldcup.dmpb.com.cn/api/jssdk/sign?url=' + location.href.split('#')[0],
				type: 'get',
				success: function (res) {
					wx.config({
						debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
						appId: 'wx3d857b42797b5eb4', // 必填，公众号的唯一标识
						timestamp: res.content.timestamp, // 必填，生成签名的时间戳
						nonceStr: res.content.nonceStr, // 必填，生成签名的随机串
						signature: res.content.signature,// 必填，签名，见附录1
						jsApiList: ['onMenuShareAppMessage'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
					});
					wx.ready(function () {   //需在用户可能点击分享按钮前就先调用
					    wx.onMenuShareAppMessage({ 
					        title: '聚首京华，携手同行', // 分享标题
					        desc: '奖品丰厚，谁将会成为下一位锦鲤？', // 分享描述
					        link: 'https://worldcup.dmpb.com.cn/front/award.html?v=' + new Date().getTime(), // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					        imgUrl: 'https://wujing123.oss-cn-beijing.aliyuncs.com/WechatIMG60.jpeg?x-oss-process=style/mystyle' // 分享图标
						});
					});
				},
				error: function () {
					alert('获取jssdk失败');
				}
			})
		},
		loadImages: function (callback) {
			var me = this;
			IMAGE_URLS.forEach(function (url) {
				var image = new Image();
				image.onload = function(){
					me.imgcount++;
					$('.progress_status').html(Math.floor(me.imgcount / IMAGE_URLS.length * 100) + '%');
					if (me.imgcount == IMAGE_URLS.length) {
						// $('.loader').hide();
						// $('#start').show().on('click', function () {
						// 	$('#page2').show().siblings().hide();
						// 	window.isPlayReady++;
						// 	if (window.isPlayReady == 2) {
						// 		me.playVoice(1);
						// 	}
						// })
						setTimeout(function () {
							$('#page2').show().siblings().hide();
							window.isPlayReady++;
							if (window.isPlayReady == 2) {
								me.playVoice(1);
							}
						}, 500);
					}
				};
				image.onerror = function(){};
				image.src = url;
			});
		},
		run: function () {
			var me = this;
			var $list = $('.chat_list');
			this.isRun = true;
			this.I = setInterval(function () {
				var text = MESSAGE_LIST[me.count-1];
				while(!text) {
					me.count++;
					text = MESSAGE_LIST[me.count-1];
				}
				if (text) {
					me.addChatItem(text, me.count);
					if (/img/.test(text)) {
						setTimeout(function () {
							me.playVoice(1);
							me.scroll.refresh();
							me.scroll.scrollToElement($('.chat_item').last().get(0), 500);
						}, 200);
					}else{
						me.playVoice(1);
						me.scroll.refresh();
						me.scroll.scrollToElement($('.chat_item').last().get(0), 500);
					}
				}
				me.count++;
				if (/WechatIMG141/.test(text)) {
					me.pause();
					setTimeout(function () {
						me.run();
					}, 1500);
				}
				if (me.count > MESSAGE_LIST.length) {
					clearInterval(me.I);
					$('.chat_all').off().hide();
				}
			}, 1000);
		},
		pause: function () {
			this.isRun = false;
			clearInterval(this.I);
		},
		initScroll: function () {
			var me = this;
			var scroll = new BScroll('.chat_bd', {
			    scrollY: true
			});
			me.scroll = scroll;
		},
		addChatItem: function (text, count) {
			if (text == 1) {
				$('.chat_list').append('<div class="chat_item chat_item_right" id="chat'+count+'"><div class="chat_item_img"></div><div class="chat_item_package"><img src="https://wujing123.oss-cn-beijing.aliyuncs.com/moneyFromMe.png?x-oss-process=style/mystyle" class="bg"><img src="https://wujing123.oss-cn-beijing.aliyuncs.com/bonus.jpg?x-oss-process=style/mystyle" class="money"><div class="title"><div>恭喜发财，大吉大利！</div><div>领取红包</div></div><div class="type">微信红包</div></div></div>');
			}else if (text == 2) {
				$('.chat_list').append('<div class="chat_time_wrap" id="chat'+count+'"><div class="chat_time">对方领取了你的红包</div></div>');
			}else if (text == 3) {
				$('.chat_list').append('<div class="chat_item chat_item_left" id="prevewBtn" id="chat'+count+'"><div class="chat_item_img"></div><div class="chat_item_text">合工大2018青年校友会奖品概览<div class="chat_item_text_footer">丰厚到让你腿软！<img src="https://wujing123.oss-cn-beijing.aliyuncs.com/ADIIABACGAAgpIbQ3wUo8KOU0gQwigU4igU%21450x450.jpg.webp?x-oss-process=style/mystyle"></div></div></div>');
				$('#prevewBtn').on('touchend', function () {
					$('#page4').show().css('transform', 'translateY(0)');
				});
			}else {
				$('.chat_list').append('<div class="chat_item '+(this.count%2?'chat_item_left':'chat_item_right')+'"><div class="chat_item_img"></div><div class="chat_item_text">'+text+'</div></div>');
			}
		},
		playVoice: function (type) {
			var audio = new Audio();
			try {
				audio.src = ['https://wujing123.oss-cn-beijing.aliyuncs.com/notification.mp3','https://wujing123.oss-cn-beijing.aliyuncs.com/click.mp3'][type-1];
				audio.load();
				audio.play();
			}catch (err) {
				console.log(err);
			}
		},
		bindEvent: function () {
			var me = this;
			var pos = {
				x: 0,
				y: 0,
				curx: 0,
				cury: 0
			};
			$('.chat_all').on('click', function () {
				me.pause();
				MESSAGE_LIST.forEach(function (text, index) {
					if (index > me.count - 2) {
						if (text) {
							me.addChatItem(text);
						}
						me.count++;
					}
				});
				$('.chat_all').off().hide();
				setTimeout(function (){
					me.scroll.refresh();
					me.scroll.scrollToElement($('.chat_item').last().get(0), 500);
				}, 350);
			});
			// $('.lock_btn').on('touchstart', function (e) {
			// 	var touch = e.touches[0];
			// 	pos.y = touch.pageY;
			// 	pos.x = touch.pageX;
			// });
			// $('.lock_btn').on('touchmove', function (e) {
			// 	var touch = e.touches[0];
			// 	pos.cury = touch.pageY;
			// 	pos.curx = touch.pageX;
			// });
			// $('.lock_btn').on('touchend', function (e) {
			// 	if (pos.curx - pos.x > 100) {
			// 		me.playVoice(2);
			// 		$('#page3').show().siblings().hide();
			// 		pos.x = 0;
			// 		pos.y = 0;
			// 		pos.curx = 0;
			// 		pos.cury = 0;
			// 		me.run();
			// 	}
			// });
			$('.lock_btn').on('click', function (e) {
				me.playVoice(2);
				$('#page3').show().siblings().hide();
				me.run();
			});
			$('.btn_return').on('click', function () {
				$('#page4').css('transform', '');
			});
			document.addEventListener('WeixinJSBridgeReady', function() {
		        window.isPlayReady++;
		        if (window.isPlayReady == 2) {
		        	me.playVoice(1);
		        }
		    })
		}
	}

	var app = new App();
	window.app = app;
	app.init();
})


/**
 * todolist:
 * 2、转发到朋友圈时的显示
 * 3、动画的衔接
 */
